﻿using HostAssistantBLL;
using HostAssistantCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HostAssistant
{
    public partial class mysqlrockbak : U1Form
    {
        string id = "";
        string name = "";
        string dbname = "";
        string bakname = "";
        public mysqlrockbak(string _id)
        {
            id = _id;
            InitializeComponent();
        }
        private void addsites_Load(object sender, EventArgs e)
        {
            var SqlInfo = dbbll.GetSqlInfo(id);
            name = SqlInfo?.Mdbname ?? "";
            dbname = SqlInfo?.Mdbname ?? "";
            lblname.Text = SqlInfo?.Mdbname ?? "";
            String path = PathSetbll.GetPathSet()?.DbBakPath ?? "";
            bakname = path + "\\mysql\\" + dbname + "\\";


            var files = Directory.GetFiles(path+"\\mysql\\"+ dbname+"\\", "*.sql");
          
            if(files!=null && files.Length>0)
            {
                foreach (var file in files)
                {
                    comboBoxEdit1.Properties.Items.Add(Path.GetFileName(file));

                }
            }
         
        }

        #region 返回
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 还原数据库
        void Addmssql()
        {
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");


            if (comboBoxEdit1.Text == "选择文件")
            {
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择要恢复的备份文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
          
            string password = "";
            string port = "";
            var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            var mysql = set.FirstOrDefault(o => o.type == "mysql");
            string ip = mysql?.ip ?? "";
            port = mysql?.port ?? "";
            string user = mysql?.username ?? "";
            password = mysql?.userpwd ?? "";

            string mpath = mysql?.Path ?? "";

            MySqlDbHelper mdb = new MySqlDbHelper();
            mdb.Initialize(ip, "mysql", user, password, port);

            if (mdb.ConnectionTest() == null)
            {
                NLogger.Info("准备还原数据库：" + dbname);

                string sqlspath = bakname;//路径
                bool beforebackup = true;//恢复前备份

                NLogger.Info("开启单一模式：" + dbname);
                string filename = comboBoxEdit1.Text;
                string sqlpath = sqlspath + "\\" + filename;
                string db_backup_name = dbname;


                if (File.Exists(sqlpath))
                {

                    string errorCode = dbbll.RebackupSql(mpath, port, ip, user, password, beforebackup, sqlpath, db_backup_name);//执行恢复过程
                    NLogger.Info("errorCode:" + errorCode ?? "");

                    if (errorCode == null)
                    {
                        splashScreenManager1.CloseWaitForm();
                        DevExpress.XtraEditors.XtraMessageBox.Show("还原完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        splashScreenManager1.CloseWaitForm();
                        DevExpress.XtraEditors.XtraMessageBox.Show(errorCode, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }






                }



            }
            else
            {


                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("未能连接上数据库,请检查数据库账号密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);



                return;
            }



        }
        private void simpleButton5_Click(object sender, EventArgs e)
        {

            try
            {
                OpHelper.CallWithTimeout(Addmssql, 600000);

            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

          

        }
        #endregion 
    }
}